<?php

class GameController extends Zend_Controller_Action
{

    public function init()
    {
    	session_start();

		if(!isset($_SESSION[yatzy][game]))
		{	
			$auth = Zend_Auth::getInstance();
			if ($auth->hasIdentity() == true){
				$result = new Default_Model_Yatzy_Result();
				$userId = $auth->getIdentity()->spagooy_user_id;
				$result->setUserId($userId);
				if ($resultId = $result->hasUnfinnishedGame($userId))
				{				
					$result = new Default_Model_Yatzy_Result();
					$result = $result->getUnfinnishedGame($resultId);
					$game = new Default_Model_Yatzy_Game();
					$game->setResult($result);
					$game->setDiceThrow($result->getCompleteDiceThrows());
					$game->getDie1()->setValue(substr($result->getDice(),0,1));
					$game->getDie2()->setValue(substr($result->getDice(),1,1));
					$game->getDie3()->setValue(substr($result->getDice(),2,1));
					$game->getDie4()->setValue(substr($result->getDice(),3,1));
					$game->getDie5()->setValue(substr($result->getDice(),4,1));
					$_SESSION[yatzy][game] = Serialize($game);
				}
				else
				{
					$_SESSION[yatzy][game] = Serialize(new Default_Model_Yatzy_Game());				
				}	
			}
			else
			{
				$_SESSION[yatzy][game] = Serialize(new Default_Model_Yatzy_Game());
			}
		}
    }

    public function indexAction()
    {
		if (isset($_POST['ajax']))
		{
			$this->_helper->layout->disableLayout();
		}
    	$game = unserialize($_SESSION[yatzy][game]);		
		$result = $game->getResult();	
		
		$auth = Zend_Auth::getInstance();
		if ($auth->hasIdentity() == false){
			$this->view->infoMessage = Zend_Registry::get('translate')->_('MUST_BE_LOGGEDIN_TO_SAVE_RES');
		}
		
		if($this->getRequest()->isPost())
		{
			$data = $_POST;			
			
			if (isset($data['ajax']))
			{
				$this->_helper->layout->disableLayout();
			}

			if (isset($data['save']) == true) {
				if (isset($data['setPoints']) == true) {
					$row = $data['setPoints'];
					$getFunction = 'get' . $row;
					$checkFunction = 'check' . $row;
					$setFunction = 'set' . $row;
					if ($result->$getFunction() == null)
					{
						$result->$setFunction($game->$checkFunction());
						$result->addSum($game->$checkFunction());
						$game->setDiceThrow(0);
						$result->addCompleteRound();

						$auth = Zend_Auth::getInstance();
						// första omgången, inloggad
						if ($result->getCompleteRounds() == 1 && $auth->hasIdentity() == true) {
							$result->setFinnished(false);
							$result->setCompleteDiceThrows($game->getDiceThrow());
							$result->setDice($game->getDie1()->getValue() . $game->getDie2()->getValue() . $game->getDie3()->getValue() . $game->getDie4()->getValue() . $game->getDie5()->getValue());
							if ($result->getId() != null)
							{
								$result->save();
							}
							else
							{
						    	$id = $result->save();
								$result->setId($id);
							}
							
						// första omgången, ej inloggad							
						} elseif ($result->getCompleteRounds() == 1 && $auth->hasIdentity() == false) {

						// sista omgången, inloggad	
						} elseif ($result->getCompleteRounds() == 15 && $auth->hasIdentity() == true) {
					    	$result->setFinnished(true);
							$result->setCompleteDiceThrows($game->getDiceThrow());
							$result->setDice($game->getDie1()->getValue() . $game->getDie2()->getValue() . $game->getDie3()->getValue() . $game->getDie4()->getValue() . $game->getDie5()->getValue());
							$result->save();
							$this->view->message="Ditt resultat har sparats och är " . $result->getSum();							
						
						// sista omgången, ej inloggad	
						} elseif ($result->getCompleteRounds() == 15 && $auth->hasIdentity() == false) {
							$this->view->message="Ditt resultat är " . $result->getSum();							
							
						// omgång 2-14, inloggad	
						} elseif ($auth->hasIdentity() == true) {
							$result->setCompleteDiceThrows($game->getDiceThrow());
							$result->setDice($game->getDie1()->getValue() . $game->getDie2()->getValue() . $game->getDie3()->getValue() . $game->getDie4()->getValue() . $game->getDie5()->getValue());
					    	$result->save();

						// omgång 2-14, ej inloggad	
						} elseif ($auth->hasIdentity() == false) {
							
						}
						
						if ($result->getCompleteRounds() < 15) {
							$this->view->gameFinnished = true;
						} else {
							$this->view->gameFinnished = false;
						}
					}
					else
					{
						// felmeddelande. något gick fel eller fusk.
					}
				} else {
					// felmeddelande. måste klicka på radiokanpp för sparaknappen
				}
			} else {			
				if ($game->getDiceThrow() < 3){	
					$game->play(!isset($data[saveDie1]),
								!isset($data[saveDie2]),
								!isset($data[saveDie3]),
								!isset($data[saveDie4]),
								!isset($data[saveDie5]));
				} else {
					
				}
				$result->setCompleteDiceThrows($game->getDiceThrow());
				$result->setDice($game->getDie1()->getValue() . $game->getDie2()->getValue() . $game->getDie3()->getValue() . $game->getDie4()->getValue() . $game->getDie5()->getValue());
				$result->setFinnished(false);

				if($auth->hasIdentity() == true)
				{
					$userId = $auth->getIdentity()->spagooy_user_id;
					$result->setUserId($userId);
	
					if ($result->getId() != null)
					{
						$result->save();
					}
					else
					{
				    	$id = $result->save();
						$result->setId($id);
					}
				}
			}
		}      
        $this->view->die1 = $game->getDie1()->getValue();
		$this->view->die1checked = isset($data[saveDie1]);
		$this->view->die2 = $game->getDie2()->getValue();
		$this->view->die2checked = isset($data[saveDie2]);
		$this->view->die3 = $game->getDie3()->getValue();
		$this->view->die3checked = isset($data[saveDie3]);
		$this->view->die4 = $game->getDie4()->getValue();
		$this->view->die4checked = isset($data[saveDie4]);
		$this->view->die5 = $game->getDie5()->getValue();
		$this->view->die5checked = isset($data[saveDie5]);
		$this->view->diceThrow = $game->getDiceThrow();
		
		if($game->getDiceThrow() > 2 || $game->getDiceThrow() == 0)
		{
			$this->view->saveDice = " disabled=\"disabled\"";
		}
		else
		{
			$this->view->saveDice = "";
		}
				
		
		//börjar fylla i res.-tabellen
		
		if ($result->getOnes() === null)
		{
			$this->view->ones = $game->checkOnes();
			$this->view->onesRes = false;
		}
		else
		{
			$this->view->ones = $result->getOnes();
			$this->view->onesRes = true;
		}
		
		if ($result->getTwos() === null)
		{
			$this->view->twos = $game->checkTwos();
			$this->view->twosRes = false;
		}
		else
		{
			$this->view->twos = $result->getTwos();
			$this->view->twosRes = true;
		}
		
		if ($result->getThrees() === null)
		{
			$this->view->threes = $game->checkThrees();
			$this->view->threesRes = false;
		}
		else
		{
			$this->view->threes = $result->getThrees();
			$this->view->threesRes = true;
		}
		
		if ($result->getFours() === null)
		{
			$this->view->fours = $game->checkFours();
			$this->view->foursRes = false;
		}
		else
		{
			$this->view->fours = $result->getFours();
			$this->view->foursRes = true;
		}
		
		if ($result->getFives() === null)
		{
			$this->view->fives = $game->checkFives();
			$this->view->fivesRes = false;
		}
		else
		{
			$this->view->fives = $result->getFives();
			$this->view->fivesRes = true;
		}
		
		if ($result->getSixes() === null)
		{
			$this->view->sixes = $game->checkSixes();
			$this->view->sixesRes = false;
		}
		else
		{
			$this->view->sixes = $result->getSixes();
			$this->view->sixesRes = true;
		}
		
		$this->view->subSum = $result->getSubSum();
		$this->view->bonus = $result->getBonus();	
			
		if ($result->getPair() === null)
		{
			$this->view->pair = $game->checkPair();
			$this->view->pairRes = false;
		}
		else
		{
			$this->view->pair = $result->getPair();
			$this->view->pairRes = true;
		}

		if ($result->getTwoPair() === null)
		{
			$this->view->twopair = $game->checkTwoPair();
			$this->view->twopairRes = false;
		}
		else
		{
			$this->view->twopair = $result->getTwoPair();
			$this->view->twopairRes = true;
		}

		if ($result->getThreeOfAKind() === null)
		{
			$this->view->threeofakind = $game->checkThreeOfAKind();
			$this->view->threeofakindRes = false;
		}
		else
		{
			$this->view->threeofakind = $result->getThreeOfAKind();
			$this->view->threeofakindRes = true;
		}
		
		if ($result->getFourOfAKind() === null)
		{
			$this->view->fourofakind = $game->checkFourOfAKind();
			$this->view->fourofakindRes = false;
		}
		else
		{
			$this->view->fourofakind = $result->getFourOfAKind();
			$this->view->fourofakindRes = true;
		}
		
		if ($result->getFullHouse() === null)
		{
			$this->view->fullhouse = $game->checkFullHouse();
			$this->view->fullhouseRes = false;
		}
		else
		{
			$this->view->fullhouse = $result->getFullHouse();
			$this->view->fullhouseRes = true;
		}
		
		if ($result->getSmallStraight() === null)
		{
			$this->view->smallStright = $game->checkSmallStraight();
			$this->view->smallStrightRes = false;
		}
		else
		{
			$this->view->smallStright = $result->getSmallStraight();
			$this->view->smallStrightRes = true;
		}
		
		if ($result->getBigStraight() === null)
		{
			$this->view->bigStright = $game->checkBigStraight();
			$this->view->bigStrightRes = false;
		}
		else
		{
			$this->view->bigStright = $result->getBigStraight();
			$this->view->bigStrightRes = true;
		}

		if ($result->getChance() === null)
		{
			$this->view->chance = $game->checkChance();
			$this->view->chanceRes = false;
		}
		else
		{
			$this->view->chance = $result->getChance();
			$this->view->chanceRes = true;
		}

		if ($result->getYatzy() === null)
		{
			$this->view->yatzy = $game->checkYatzy();
			$this->view->yatzyRes = false;
		}
		else
		{
			$this->view->yatzy = $result->getYatzy();
			$this->view->yatzyRes = true;
		}
		
		$this->view->sum = $result->getSum();

		
		if ($result->getCompleteRounds() < 15) {
			$_SESSION[yatzy][game] = serialize($game);
		} else {
			$_SESSION[yatzy][game] = null;
		}
	}
}

